package com.chris.libs.http.x.v1;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * create by Chris Chan
 * create on 2019/7/9 23:17
 * use for :
 */
public class NetClient {
    public static OkHttpClient client;
    public static Retrofit.Builder builder;
    public static String timePattern = "yyyy-MM-dd HH:mm:ss";
    public static String timezone = "GMT+8";

    private static LinkedList<Interceptor> interceptors = new LinkedList<>();
    // 默认拦截器
    private static Interceptor defaultInterceptor;

    public static void setDefaultInterceptor(Interceptor defaultInterceptor) {
        NetClient.defaultInterceptor = defaultInterceptor;
    }

    public static LinkedList<Interceptor> getInterceptors() {
        return interceptors;
    }

    public static void setInterceptors(LinkedList<Interceptor> interceptors) {
        NetClient.interceptors = interceptors;
    }

    public static void addInterceptor(Interceptor interceptor) {
        NetClient.interceptors.add(interceptor);
    }

    /**
     * 创建retrfit对象
     *
     * @param baseUrl
     * @return
     */
    public static Retrofit getRetrofit(String baseUrl) {
        //retrofit规范，url必须以/结束
        if (!baseUrl.endsWith("/")) {
            baseUrl = baseUrl + "/";
        }
        //如果从第七个字符开始有双斜杠，则表示构建时在前面加了/，这个时候做个替换
//        if (baseUrl.indexOf("//", 7) > 0) {
//            baseUrl = baseUrl.replaceAll("//", "/").replaceFirst("/", "//");
//        }
        Gson gson = new GsonBuilder().setDateFormat(timePattern).create();
        if (builder == null) {
            builder = new Retrofit.Builder()
                    .client(getClient())
                    .addConverterFactory(GsonConverterFactory.create(gson));
        }
        return builder.baseUrl(baseUrl).build();
    }

    /**
     * 创建okHttp客户端
     *
     * @return
     */
    public static OkHttpClient getClient() {
        if (client == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    //.addInterceptor(new NetInterceptor())
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .retryOnConnectionFailure(true);
            // 添加默认拦截器
            if (null != NetClient.defaultInterceptor) {
                builder.addInterceptor(NetClient.defaultInterceptor);
            }
            // 添加拦截器链
            if (null != NetClient.interceptors && NetClient.interceptors.size() > 0) {
                for (Interceptor interceptor : NetClient.interceptors) {
                    builder.addInterceptor(interceptor);
                }
            }
            client = builder.build();

        }
        return client;
    }
}
